package com.linwen.excise.niuke._40;

import java.util.Scanner;

/**
 * @author wlw
 * @date 2023/3/12 11:04 PM
 */
public class _39判断两个IP是否属于同一子网 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str1 = scanner.nextLine();
        String str2 = scanner.nextLine();
        String str3 = scanner.nextLine();
        boolean b = checkParam(str1, str2, str3);
        if(b){
            if(checkIsSameNet(str1,str2,str3)){
                System.out.println(0);
            }else {
                System.out.println(2);
            }
        }else {
            System.out.println(1);
        }
    }

    private static boolean checkIsSameNet(String str1, String str2, String str3) {
        String[] split1 = str1.split("\\.");
        String[] split2 = str2.split("\\.");
        String[] split3 = str3.split("\\.");
        for (int i = 0; i < 4; i++) {
            if((Integer.parseInt(split1[i])& Integer.parseInt(split2[i]))
                    != (Integer.parseInt(split1[i])& Integer.parseInt(split3[i]))){
                return false;
            }
        }
        return true;
    }

    private static boolean checkParam(String str1, String str2, String str3) {
        if(checkMask(str1)&&checkIp(str2)&&checkIp(str3)){
            return true;
        }
        return false;
    }

    private static boolean checkMask(String str1) {
        String[] split = str1.split("\\.");
        if(split.length==4){
            int i1 = Integer.parseInt(split[0]);
            int i2 = Integer.parseInt(split[1]);
            int i3 = Integer.parseInt(split[2]);
            int i4 = Integer.parseInt(split[3]);
            if(i1>=i2&&i2>=i3&&i3>=i4&&i1<=255&&i4>=0){
                return true;
            }else {
                return false;
            }
        }
        return false;
    }

    private static boolean checkIp(String str1) {
        String[] split = str1.split("\\.");
        if(split.length==4){
            for (int i = 0; i < split.length; i++) {
                int anInt = Integer.parseInt(split[i]);
                if(anInt<0||anInt>255){
                    return false;
                }
            }
            return true;
        }
        return false;
    }
}
